<template>
	<view class="topUp">
		<view class="top_up_num_box">
			<view class="fn-sz-30 fn-cl-666666 mar-bottom-40">充值金额</view>
			<view class="top_up_input_num_box">
				<view class="price_symbol">￥</view>
				<view class="top_up_input_box"><input type="text" v-model="topUpObj.price" /></view>
			</view>
		</view>
		<view class="area-title">支付方式</view>
		<view class="pay_list">
			<view class="wechat_pay" @tap="weiXinPay">
				<image class="icon-36 mar-right-8" src="../../static/images/icon/icon_wechat_pay.png" mode=""></image>
				<span class="fn-sz-30 fn-cl-FFFFFF">微信支付</span>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				topUpObj: {
					pay_type: 'mini',
					price: ''
				},
				upType: ''
			};
		},
		onLoad(val) {
			console.log(val, 'gaga');
			if (val.type == 'coin') {
				this.upType = 'coin';
			}
		},
		methods: {
			weiXinPay() {
				let api = this.upType == 'coin' ? 'rechargeCoin' : 'recharge'
				this.$api[api]({
					...this.topUpObj
				}).then(res => {
					console.log(res, '微信支付结果');
					if (res.code == 1) {
						uni.requestPayment({
							provider: 'wxpay',
							timeStamp: res.data.pay_info.timeStamp,
							nonceStr: res.data.pay_info.nonceStr,
							package: res.data.pay_info.package,
							signType: res.data.pay_info.signType,
							paySign: res.data.pay_info.paySign,
							success: function(rex) {
								console.log('success:' + JSON.stringify(rex));
								setTimeout(() => {
									uni.switchTab({
										url: `/pages/mine/mine`
									});
								}, 800);
							},
							fail: function(err) {
								console.log('fail:' + JSON.stringify(err));
								// uni.switchTab({
								// 	url: '/pages/home/home'
								// });
							}
						});
					}
				});
			}
		}
	};
</script>

<style lang="scss">
	.topUp {
		padding: 24rpx;
		min-height: 100vh;
		background-color: #f9f9f9;
		box-sizing: border-box;

		.top_up_num_box {
			background: #ffffff;
			border-radius: 20rpx;
			padding: 40rpx 36rpx;
			margin-bottom: 50rpx;

			.top_up_input_num_box {
				display: flex;
				border-bottom: 1rpx solid #eeeeee;
				padding-bottom: 15rpx;

				.price_symbol {
					font-size: 30rpx;
					color: #333;
					padding-top: 55rpx;
				}

				.top_up_input_box {
					height: 100rpx;

					input {
						height: 100rpx;
						font-size: 90rpx;
						color: #333;
					}
				}
			}
		}

		.pay_list {
			.wechat_pay {
				height: 90rpx;
				background: #09bb07;
				border-radius: 45rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-bottom: 40rpx;
			}
		}
	}
</style>